package com.zw.yzk.kabu.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.GoodsOrderAdapter;
import com.zw.yzk.kabu.base.BaseFragment;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerGoodsOrderComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.GoodsOrderModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.model.GoodsOrderModel;
import com.zw.yzk.kabu.presenter.GoodsOrderPresenter;
import com.zw.yzk.kabu.view.activity.GoodsOrderDetailsActivity;
import com.zw.yzk.kabu.view.activity.LogisticsActivity;
import com.zw.yzk.kabu.view.activity.MyGoodsOrderActivity;
import com.zw.yzk.kabu.view.activity.RefundActivity;
import com.zw.yzk.kabu.view.view.GoodsOrderView;
import com.zw.yzk.kabu.view.widget.CustomAlertDialog;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/8.
 */

public class GoodsOrderFragment extends BaseFragment implements GoodsOrderView, GoodsOrderAdapter.OnOrderItemClickedListener {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.order_list)
    RecyclerView orderList;

    @Inject
    GoodsOrderPresenter presenter;
    private int orderType;
    private MyGoodsOrderActivity activity;
    private GoodsOrderAdapter adapter;

    @Override
    protected int setContent() {
        return R.layout.fragment_goods_order;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MyGoodsOrderActivity) context;
    }

    @Override
    protected void initView(View view) {
        initRecyclerView();
    }

    @Override
    public void loadData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.checkOrderState();
    }

    @Override
    public void initComponent() {
        DaggerGoodsOrderComponent.builder()
                .applicationComponent(activity.getApplicationComponent())
                .activityModule(new ActivityModule(activity))
                .goodsOrderModule(new GoodsOrderModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getOrderListSuccessfully(List<GoodsOrderModel> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
    }

    @Override
    public void getOrderListFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    @Override
    public void confirmReceiveSuccess() {
        getGoodsOrderList(0);
    }

    @Override
    public void queryOrderStatusSuccess() {
        getGoodsOrderList(0);
    }

    @Override
    public void pay(GoodsOrderModel model) {
        payOrder(model);
        LoadingDialogManager.getInstance().showLoadingDialog(activity);
    }

    @Override
    public void urge(GoodsOrderModel model) {
        new AlertDialog.Builder(activity)
                .setCancelable(true)
                .setMessage(R.string.urge_oder)
                .setPositiveButton(R.string.confirm, null)
                .show();
    }

    @Override
    public void viewLogistics(GoodsOrderModel model) {
        Intent intent = new Intent(activity, LogisticsActivity.class);
        intent.putExtra(ActivityConstant.LOGISTICS_ID, model.getLogisticId());
        startActivity(intent);
    }

    @Override
    public void confirmReceive(final GoodsOrderModel model) {
        new CustomAlertDialog.Builder(activity, R.layout.dialog_logout)
                .setTitle(R.id.title, activity.getResources().getString(R.string.confirm_receive_title))
                .setContent(R.id.content, activity.getResources().getString(R.string.confirm_receive_content))
                .setNegativeButton(R.id.cancel, activity.getResources().getString(R.string.cancel))
                .setPositiveButton(R.id.confirm, activity.getResources().getString(R.string.confirm))
                .setOnPositiveButtonClickerListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        confirmReceive(model.getOrderId());
                    }
                })
                .setCancelable(false).build().show();
    }

    @Override
    public void applyCancel(GoodsOrderModel model) {
        Intent intent = new Intent(activity, RefundActivity.class);
        intent.putExtra(ActivityConstant.REFUND_ORDER_ID, model.getOrderId());
        startActivity(intent);
    }

    @Override
    public void viewDetails(GoodsOrderModel model) {
        Intent intent = new Intent(activity, GoodsOrderDetailsActivity.class);
        intent.putExtra(ActivityConstant.GOODS_ORDER_DETAILS_ID, model.getOrderId());
        intent.putExtra(ActivityConstant.GOODS_ORDER_DETAILS_TYPE, model.getOrderType());
        startActivity(intent);
    }


    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        orderList.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        adapter = new GoodsOrderAdapter(null);
        adapter.setOnOrderItemClickedListener(this);
        orderList.setAdapter(adapter);
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getGoodsOrderList(0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                        getGoodsOrderList(adapter.getItemCount());
                    }
                }).build();
    }

    /**
     * 设置展示的订单类型
     *
     * @param type 订单类型
     */
    public void setOrderType(int type) {
        this.orderType = type;
    }

    /**
     * 获取订单列表
     *
     * @param start 订单开始索引
     */
    private void getGoodsOrderList(int start) {
        presenter.getGoodsOrderList(start, orderType);
    }

    /**
     * 确认订单
     *
     * @param orderId 订单id
     */
    private void confirmReceive(String orderId) {
        presenter.confirmReceive(orderId);
    }

    /**
     * 付款
     *
     * @param model
     */
    private void payOrder(GoodsOrderModel model) {
        presenter.pay(model.getOrderId());
    }

}
